﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
    /// <summary>
    /// 公告数据访问层
    /// </summary>
    public class Notice
    {
        /// <summary>
        /// 增加公告
        /// </summary>
        /// <param name="noticeInfo">公告实体类</param>
        /// <returns>返回公告ID</returns>
        public int Add(NoticeInfo noticeInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSER INTO web_Notice(");
            strSql.Append("Contents,Pic,CId,MId,CreateTime)");
            strSql.Append(" VALUES (");
            strSql.Append("@Contents,@Pic,@TheType,@MId,@CreateTime)");
            strSql.Append(";SELECT @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Contents", SqlDbType.VarChar,4000),
					new SqlParameter("@Pic", SqlDbType.VarChar,512),
					new SqlParameter("@CId", SqlDbType.Int,4),
					new SqlParameter("@MId", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime)};
            parameters[0].Value = noticeInfo.Contents;
            parameters[1].Value = noticeInfo.Pic;
            parameters[2].Value = noticeInfo.CId;
            parameters[3].Value = noticeInfo.MId;
            parameters[4].Value = noticeInfo.CreateTime;

            object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 修改公告
        /// </summary>
        /// <param name="noticeInfo">公告实体类</param>
        /// <returns>返回是否成功</returns>
        public bool Update(NoticeInfo noticeInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE web_Notice SET ");
            strSql.Append("Contents=@Contents,");
            strSql.Append("Pic=@Pic,CId=@CId,MId=@MId,CreateTime=@CreateTime ");
            strSql.Append("WHERE Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Contents", SqlDbType.VarChar,4000),
					new SqlParameter("@Pic", SqlDbType.VarChar,512),
					new SqlParameter("@TheType", SqlDbType.Int,4),
					new SqlParameter("@MId", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = noticeInfo.Contents;
            parameters[1].Value = noticeInfo.Pic;
            parameters[2].Value = noticeInfo.CId;
            parameters[3].Value = noticeInfo.MId;
            parameters[4].Value = noticeInfo.CreateTime;
            parameters[5].Value = noticeInfo.Id;

            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一个公告
        /// </summary>
        /// <param name="id">公告ID</param>
        /// <returns>返回是否成功</returns>
        public bool Delete(int id)
        {
            string strSql = "DELETE FROM ma_Manger WHERE Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
